package com.ftg.learn.springbootmq.topic.producer;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 主题模式消息生产者
 */
@Component
public class TopicProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送订单相关消息
     *
     * @param message 消息内容
     */
    public void sendOrderMessage(String type, Object message) {
        String routingKey = "order." + type;
        Map<String, Object> msg = new HashMap<>();
        msg.put("type", "order");
        msg.put("action", type);
        msg.put("data", message);
        msg.put("timestamp", System.currentTimeMillis());

        rabbitTemplate.convertAndSend("topic.exchange", routingKey, msg);
    }

    /**
     * 发送用户相关消息
     *
     * @param message 消息内容
     */
    public void sendUserMessage(String type, Object message) {
        String routingKey = "user." + type;
        Map<String, Object> msg = new HashMap<>();
        msg.put("type", "user");
        msg.put("action", type);
        msg.put("data", message);
        msg.put("timestamp", System.currentTimeMillis());

        rabbitTemplate.convertAndSend("topic.exchange", routingKey, msg);
    }

    /**
     * 发送商品相关消息
     *
     * @param message 消息内容
     */
    public void sendProductMessage(String type, Object message) {
        String routingKey = "product." + type;
        Map<String, Object> msg = new HashMap<>();
        msg.put("type", "product");
        msg.put("action", type);
        msg.put("data", message);
        msg.put("timestamp", System.currentTimeMillis());

        rabbitTemplate.convertAndSend("topic.exchange", routingKey, msg);
    }
}

